﻿Shader "Combined"
{
    Properties
    {
        _BaseColor("Color",Color) = (1,1,1,1)
        _BaseMap("Texture",2D) = "white"{}
        _BumpMap("Normal Map",2D) = "bump" {}
        _BumpScale("NormalScale",Range(-1,1)) = 1
        _Gloss("Gloss",Range(0.03,1)) = 0.5
        
        [Space(30)]
        [KeywordEnum (Lambert,HalfLambert)] DIFFUSETYPE("Type", Float) = 0
        [KeywordEnum (Phong,Blinn)] SPECULARTYPE("Type", Float) = 0
    }
    SubShader
    {
        Tags
        {
            "RenderPipeline"="UniversalRenderPipeline"
            "LightMode"="UniversalForward"
            "RenderType"="Opaque"
        }
        LOD 100
        
        HLSLINCLUDE
        #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" 
        #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"

        #pragma multi_compile DIFFUSETYPE_LAMBERT DIFFUSETYPE_HALFLAMBERT
        #pragma multi_compile SPECULARTYPE_PHONG SPECULARTYPE_BLINN

        TEXTURE2D( _BaseMap);   SAMPLER(sampler_BaseMap);
        TEXTURE2D( _BumpMap);   SAMPLER(sampler_BumpMap);

        CBUFFER_START(UnityPerMaterial)
        float4 _BaseMap_ST;
        half4 _BaseColor;
        half _BumpScale;
        half _Gloss;
        CBUFFER_END
        ENDHLSL
        
        pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            struct a2v
            {
                float4 positionOS : POSITION;
                float3 normalOS : NORMAL;
                float4 tangentOS : TANGENT;
                float2 texcoord : TEXCOORD;
            };
            struct v2f
            {
                float4 positionCS : SV_POSITION;
                float3 positionWS : TEXCOORD0;
                half3 normalWS : NORMAL;
                half3 tangent : TANGENT;
                half3 bitangent : TEXCOORD1;
                half2 texcoord : TEXCOORD2;
            };
            
            v2f vert(a2v v)
            {
                v2f o;
                VertexPositionInputs vertexInput = GetVertexPositionInputs(v.positionOS.xyz);
                o.positionCS = vertexInput.positionCS;
                o.positionWS = vertexInput.positionWS;
                VertexNormalInputs normalInput = GetVertexNormalInputs(v.normalOS, v.tangentOS);
                o.normalWS = normalInput.normalWS;
                o.tangent = normalInput.tangentWS;
                o.bitangent = normalInput.bitangentWS;
                o.texcoord = TRANSFORM_TEX(v.texcoord, _BaseMap); 
                return o;
            }

            half4 frag(v2f i):SV_Target
            {
                Light light = GetMainLight();
                half3 normalTS = UnpackNormalScale(SAMPLE_TEXTURE2D(_BumpMap, sampler_BumpMap, i.texcoord),_BumpScale);
                half3 normalWS = normalize(TransformTangentToWorld(normalTS, half3x3(i.tangent, i.bitangent, i.normalWS)));
                half3 viewDir = normalize(GetCameraPositionWS() - i.positionWS);
                half3 reflectDir = normalize(reflect(-light.direction,normalWS));
                half3 halfDir = normalize(light.direction + viewDir); 


                half4 albedo = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, i.texcoord);
                #ifdef DIFFUSETYPE_LAMBERT 
                    half3 diffuse = _BaseColor.rgb * saturate(dot(normalWS, light.direction))* light.color;
                #elif DIFFUSETYPE_HALFLAMBERT
                    half3 diffuse = _BaseColor.rgb * saturate(dot(normalWS, light.direction)* 0.5h+ 0.5h)* light.color;
                #endif
                #ifdef SPECULARTYPE_PHONG 
                    half3 specularCol = _BaseColor.rbg* pow(saturate(dot(viewDir, reflectDir)),_Gloss* 128)* light.color;
                #elif SPECULARTYPE_BLINN
                    half3 specularCol = _BaseColor.rbg* pow(saturate(dot(normalWS, halfDir)),_Gloss* 128)* light.color;
                #endif
                albedo.rgb = albedo.rgb * diffuse + specularCol;                
                return float4(albedo.rgb,1);
            }
            ENDHLSL
        }
    }
}